//
//  MutView.m
//  Select
//
//  Created by dihuijun on 17/3/17.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "MutView.h"
#import "SignalAndMutipTableViewCell.h"

static NSString *ID = @"mutipCell";
@implementation MutView

/** 有头部的初始化*/
+ (MutView *)sharedMutViewWithFrame:(CGRect)frame title:(NSString *)title
{
    MutView *view = [[MutView alloc]initWithFrame:frame HaveHeader:YES WithTitle:title];
    return view;
}
/** 无头的初始化*/
+ (instancetype)sharedViewWithFrame:(CGRect)frame{
    return [[MutView alloc]initWithFrame:frame];
}


- (instancetype)initWithFrame:(CGRect)frame HaveHeader:(BOOL)flg WithTitle:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        //1.创建表格
        [self setupTableView];
        //2.头部
        if (flg) {
            UIView *headerView = [self setupHeader_View:title];
            self.tableView.tableHeaderView = headerView;
        }
        
        
    }
    return self;
}
#pragma mark --- 1.创建表格
- (void)setupTableView{
    //初始化
    _chooseArray = [NSMutableArray array];
    
    //
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册
    [self.tableView registerClass:[SignalAndMutipTableViewCell class] forCellReuseIdentifier:ID];
    [self addSubview:self.tableView];
}
#pragma mark --- 2.setupHeader_View 头部
- (UIView *)setupHeader_View:(NSString *)title
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    ///2.1 titleLabel
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    [headerView addSubview:titleLabel];
    //布局
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(headerView.mas_left).offset(10);
        make.top.mas_equalTo(headerView.mas_top).offset(0);
        make.height.mas_equalTo(50);
    }];
    //2.按钮
    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectButton setImage:[UIImage imageNamed:@"table_UnSelect"] forState:UIControlStateNormal];
    [selectButton setImage:[UIImage imageNamed:@"table_Selected"] forState:UIControlStateSelected];
    selectButton.tag = 100;
    selectButton.userInteractionEnabled = YES;
    //添加
    [headerView addSubview:selectButton];
    //布局
    [selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(headerView.mas_right).offset(-10);
        make.top.mas_equalTo(headerView.mas_top).offset(0);
        make.height.mas_equalTo(50);
    }];
    //3.点击整行
    UIButton *seleTotallButton = [UIButton buttonWithType:UIButtonTypeCustom];
    seleTotallButton.frame = CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height);
    [seleTotallButton addTarget:self action:@selector(ClickTotallyButton:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:seleTotallButton];
    //4.分割线
    UIView *SeparateView = [[UIView alloc]initWithFrame:CGRectMake(0, 49, self.frame.size.width, 1)];
    SeparateView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [headerView addSubview:SeparateView];
    
    return headerView;
}
/**
 * 点击选中整行
 */
- (void)ClickTotallyButton:(UIButton *)button
{
    //整行选中
    _isAllSelectSwitch = YES;
    //选中后修改按钮状态
    UIButton *btn = (UIButton *)[self.tableView.tableHeaderView viewWithTag:100];
    btn.selected = !_isAllSelected;
    
    _isAllSelected = !_isAllSelected;
    if (_isAllSelected) {
        [self.chooseArray removeAllObjects];
        
        [self.chooseArray addObjectsFromArray:self.datasource];
    }else{
        [self.chooseArray removeAllObjects];
    }
    //刷新表格才会有效果
    [self.tableView reloadData];
    //传递数据出去
    _block(@"All",self.chooseArray);
    
}
#pragma mark --- <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SignalAndMutipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.titleLabel.text = self.datasource[indexPath.row];
    
       //判断是不是点击了整行选中,如果点击了整行就应该修改cell的按钮状态
    if (_isAllSelectSwitch) {
        [cell updateButtonState:_isAllSelected];
        if (indexPath.row == self.datasource.count-1) {
            _isAllSelectSwitch = NO;
        }
    }
    
    return cell;
    
}
//点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SignalAndMutipTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell updateButtonState:!cell.isSelected];
    
    //判断是否选中一行
    if (cell.isSelected) {
        [self.chooseArray addObject:cell.titleLabel.text];
    }else{
        [self.chooseArray removeObject:cell.titleLabel.text];
    }
    
    //判断是不是整个数据都被选中
    UIButton *button = (UIButton *)[self.tableView.tableHeaderView viewWithTag:100];
    if (self.chooseArray.count < self.datasource.count) {
        //没有全部选中，所以 全选按钮 状态为没选中
        _isAllSelected = NO;
        button.selected = _isAllSelected;
        
        
    }else{
        _isAllSelected = YES;
        button.selected = _isAllSelected;
    }
    
    _block(cell.titleLabel.text,self.chooseArray);
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

/** 刷新*/
- (void)reloadData{
    [self.tableView reloadData];
}
@end
